package models;

import java.util.*;
import javax.persistence.*;

import play.data.format.*;
import play.data.validation.*;

public class SeatForm {

	@Constraints.Required
	public Long session;

	@Constraints.Required
	@Formats.NonEmpty
	public String seats;

	@Constraints.Required
	@Formats.NonEmpty
	public String language;
	
	public int bypassLogin;

	public boolean isSold(Long id, String seats) {
		boolean isSold = false;
		String[] seat = seats.split(":");
		for(int i=0;i<seat.length;i++){
			if(Payment.isTicketSold(id,seat[i])){
				return true;
			}
		}
		return false;
	}

	public boolean isHold(Long id, String seats) {
		String[] seat = seats.split(":");
		List<OnHold> records = OnHold.findBySession(id);
		for(OnHold record : records){
			if(Arrays.asList(seat).contains(record.seat_no)){
				return true;
			}
		}

		return false;
	}

	public String validate() {
		if(isSold(session,seats)){
			return "The seat(s) is/are sold in the current session.";
		}

		if(isHold(session,seats)){
			return "The seat(s) is/are hold in the current session.";
		}

		return null;
	}


}

